package com.qf.utils;

import com.qf.entity.SysUserInfo;
import com.qf.pojo.BaseUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ztf
 * @Date 16:47 2021/12/30
 **/
public class SecurityUtils {
    public static Map<Thread, Object> map = new HashMap<>();

    /**
     * 获取当前操作用户
     *
     * @return
     */
    public static BaseUser getUser() {
//        SysUserInfo user = (SysUserInfo)
//                ((ServletRequestAttributes)
//                        RequestContextHolder
//                                .getRequestAttributes())
//                        .getRequest()
//                        .getSession()
//                        .getAttribute("user");
//        return user;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        BaseUser baseUser = (BaseUser) authentication.getPrincipal();
        return baseUser;
    }
}
